package com.example.test.Admin_function;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.test.R;
import com.example.test.Test.all_test;
import com.example.test.all_Myhelper.exam_Myhelper;
import com.example.test.all_Myhelper.test_Myhelper;
import com.example.test.bottom_tab.tab_main_activity;
import com.example.test.exam.Main;
import com.example.test.login.login_main_activity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class add_test extends AppCompatActivity implements View.OnClickListener {
    public test_Myhelper test_myhelper;
    public exam_Myhelper exam_myhelper;
    public EditText test_title;
    public Button select_timu,upload_test,back,test_time;
    public TextView find_timu,find_time;
    public TimePickerDialog timePickerDialog;
    public int hours,minutes,second;
    public List list1 ;//所选择的题目列表
    public List list2;//所选择的题目id
    public List<String> t_title ;//题库中的所有题
    public List<String> t_id ;//题库中所有题的id
    public List<Boolean> t_mark ;//是否选中的标记

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_test);
//        test_myhelper = new test_Myhelper(this);
        exam_myhelper = new exam_Myhelper(this);
        test_myhelper = new test_Myhelper(this);
        init();
        t_title = new ArrayList<>(titles());//初始化title内容为题库的所有题目的题目
        t_id = new ArrayList<>(id_list());//初始化id内容为题库的所有题目的id

    }

    public void init(){
        test_title = (EditText) findViewById(R.id.test_title);
        select_timu = (Button) findViewById(R.id.select_timu);
        upload_test = (Button) findViewById(R.id.upload_test);
        find_timu = (TextView) findViewById(R.id.find_timu);
        find_time = (TextView) findViewById(R.id.find_time);
        back = (Button) findViewById(R.id.select_back);
        test_time = (Button) findViewById(R.id.test_time);
        select_timu.setOnClickListener(this);
        upload_test.setOnClickListener(this);
        test_time.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    public List<String> titles(){//获取tiku的所有题的题目
        SQLiteDatabase db;
        int i=0;
        db = exam_myhelper.getReadableDatabase();
        Cursor cursor = db.query("tiku",null,null,null,null,null,null);
        String[] all_titles = new String[cursor.getCount()];
        while (cursor.moveToNext()){
            all_titles[i] = cursor.getString(1);
            i++;
        }
        List<String> list = Arrays.asList(all_titles);
        System.out.println("测试是："+list);
        cursor.close();
        db.close();
        return list;
    }

    public List<String> id_list(){//获取题库中的所有题目的id
        SQLiteDatabase db;
        int i=0;
        db = exam_myhelper.getReadableDatabase();
        Cursor cursor = db.query("tiku",null,null,null,null,null,null);
        String[] list_id = new String[cursor.getCount()];
        while (cursor.moveToNext()){
            list_id[i] = cursor.getString(0);
            i++;
        }
        List<String> list = Arrays.asList(list_id);
        cursor.close();
        db.close();
        return list;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.select_timu){//选择题目
            alert();
        }else if(v.getId() == R.id.upload_test){//提交题
            String edi = test_title.getText().toString().trim();//编辑框内容
            char ft = find_timu.getText().toString().trim().charAt(1);//显示框判断
            String ftt = String.valueOf(ft);
            if(second==0){
                Toast.makeText(add_test.this,"请重新选择测试时间！",Toast.LENGTH_SHORT).show();
            }
            if(edi.equals("") || !ftt.equals("第")){
                Toast.makeText(add_test.this,"标题或试题不可为空！",Toast.LENGTH_SHORT).show();
            }else{
//                //获取标题
                test_title.getText();
//                System.out.println("12111111");
//                System.out.println("111"+test_title.getText().toString()+"111");
                SQLiteDatabase db;
                int i =0;
                db = test_myhelper.getReadableDatabase();
                db.execSQL("insert into test (title,timu_list,second) values (?,?,?)",new Object[]{test_title.getText().toString(),list2,second});
                db.close();
                Toast.makeText(add_test.this,"创建测试成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(add_test.this, all_test.class);
                startActivity(intent);

            }
        }else if(v.getId()==R.id.back){
            Intent test_add = new Intent(add_test.this, tab_main_activity.class);
            startActivity(test_add);
        }else if(v.getId()==R.id.test_time){
            showDailog();
        }
    }

    public void alert(){
        final String[] s = new String[titles().size()];//title
        final boolean[] a = new boolean[s.length];//状态，默认都是false
//        System.out.println(Arrays.toString(a));
        for(int i =0;i<titles().size();i++){//初始化所有的选项为false
            s[i] = t_title.get(i);//标题
        }
        System.out.println("No1------"+Arrays.toString(s));
        System.out.println("此处"+Arrays.toString(a));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("题库");//标题
        builder.setMultiChoiceItems(s, a, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            }
        });
        //把选中的挑选出来
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String[] sb = new String[titles().size()];//记录选择的条目
                String[] sz = new String[id_list().size()];//记录所选条目的id
                //把选中的条目的数据取出来
                for (int j =0;j<sz.length;j++){
                    sb[j] = "null";
                    sz[j] = "null";
                    if(a[j]){
                        sb[j] = t_title.get(j);
                        sz[j] = t_id.get(j);
                    }
                }
//                System.out.println("DDDDDD"+ Arrays.toString(sb));
                List<String> chance_timu = Arrays.asList(sb);//将选择的timu放入chance_timu中
                List<String> chance_id = Arrays.asList(sz);//将选择的timu的id放入chance_id中
                //此处有大坑
//                调用Arrays.asList()生产的List的add、remove方法时报异常，这是由Arrays.asList() 返回的市Arrays的内部类ArrayList， 而不是java.util.ArrayList。Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList，remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。java.util.ArrayList重新了这些方法而Arrays的内部类ArrayList没有重新，所以会抛出异常
                 list1 = new ArrayList(chance_timu);
                 list2 = new ArrayList(chance_id);
                for(int i=0;i<list2.size();i++){
                    if (list1.get(i).equals("null") && list2.get(i).equals("null")){
                        list1.remove(i);
                        list2.remove(i);
                        i--;
                    }
                }
//                System.out.println("No2----"+list1);
                String[] see = new String[list2.size()];
                for(int k=0;k<see.length;k++){
                    see[k] = "第"+(k+1)+"题，题目："+list1.get(k)+",编号："+list2.get(k)+"。\n";
                }
                find_timu.setText(Arrays.toString(see));//将所有已选的题目填入到显示框中
                dialog.dismiss();
                Toast.makeText(add_test.this,"选题成功！",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    public void showDailog(){
        timePickerDialog = new TimePickerDialog(this, android.app.AlertDialog.THEME_HOLO_DARK, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Log.d("测试", Integer.toString(hourOfDay));
                Log.d("测试", Integer.toString(minute));
                hours = hourOfDay;
                minutes = minute;
                second = hours*3600+minutes*60;
                System.out.println(second);
                find_time.setText("测试时长是:"+hours+"小时，"+minutes+"分钟");

                Toast.makeText(add_test.this,"测试时间是:"+hours+"小时"+minute+"分钟",Toast.LENGTH_SHORT).show();
            }
        },
                0,
                0,
                true);
        timePickerDialog.show();
        timePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }


}